// pages/order/order.js
var config = require('../../config.js');
var pagesize = 10;
var app = getApp();
var request = require('../../utils/request.js')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,//当前页面
    titles: ['全部', '待支付', '待发货', '待收货'],
    pageIndex: [1, 1, 1, 1],//记录每个页面当前的pageindex
    states: [0, 1, 2, 3],//记录每个页面当前的state
    allData: [],//全部数据
    toPayData: [],//待支付
    toSendData: [],//待发货
    toReceiveData: [],//待收货
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var index = options.index || 0;
    this.setData({
      currentTab: index
    });
    this.loadData();
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    var index = parseInt(this.data.currentTab);
    var pageindex = this.data.pageIndex;
    pageindex[index] = 1;
    this.setData({
      pageIndex: pageindex
    });
    this.loadData();
  },

  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    });
    var index = parseInt(e.currentTarget.dataset.idx);
    switch (index) {
      case 0:
        if (this.data.allData.length == 0) {
          this.loadData();
        }
        break;
      case 1:
        if (this.data.toPayData == 0) {
          this.loadData();
        }
        break;
      case 2:
        if (this.data.toSendData == 0) {
          this.loadData();
        }
        break;
      case 3:
        if (this.data.toReceiveData == 0) {
          this.loadData();
        }
        break;
    }
  },

  loadData: function () {
    var index = parseInt(this.data.currentTab);
    var state = this.data.states[index];
    var page = this.data.pageIndex[index];
    this.orderLstAction(state, page);
  },


  orderLstAction: function (state, page) {
    var self = this;
    var url = config.orderLstUrl;
    var shopid = app.globalData.shopId;
    var params = {
      state: state,
      page: page,
      size: pagesize,
      shopId: shopid
    }
    console.log(params);
    request.requestPost(url, params)
      .then(data => {
        self.setDataLst(data);
        //隐藏顶部loading
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading();
      }).catch(msg => {
        console.warn(msg)
        wx.hideLoading();
        //隐藏顶部loading
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
      });
  },

  /**
   * 设置数据源
   */
  setDataLst: function (data) {
    var index = parseInt(this.data.currentTab);
    var page = this.data.pageIndex[index];
    var tempLst = [];
    switch (index) {
      case 0:
        tempLst = this.data.allData;
        break;
      case 1:
        tempLst = this.data.toPayData;
        break;
      case 2:
        tempLst = this.data.toSendData;
        break;
      case 3:
        tempLst = this.data.toReceiveData;
        break;
    }
    if (page == 1) {
      tempLst = data;
    } else {
      tempLst.push(data);
    }
    switch (index) {
      case 0:
        this.setData({
          allData: tempLst,
        })
        break;
      case 1:
        this.setData({
          toPayData: tempLst,
        })
        break;
      case 2:
        this.setData({
          toSendData: tempLst
        })
        break;
      case 3:
        this.setData({
          toReceiveData: tempLst
        })
        break;
    }
  },


  payAction: function (e) {
    var url = config.payGoodsUrl;
    var tradeId = e.currentTarget.dataset.item;
    var openId = wx.getStorageSync('openId');
    var shopId = app.globalData.shopId;
    var params = {
      tradeId: tradeId,
      openId: openId,
      shopId: shopId
    }
    request.requestPost(url, params)
      .then(data => {
        console.log(data);
        wx.requestPayment(
          {
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.package,
            'signType': 'MD5',
            'paySign': data.paySign,
            'success': function (res) {
              console.log(res);
            },
            'fail': function (res) {
              console.log(res);
            },
            'complete': function (res) {
              console.log(res);
            }
          })
      }).catch(msg => {
        console.log(msg);
      });
  },


  onRemindAction: function (e) {
    var item = e.currentTarget.dataset.item;
    wx.showToast({
      title: '提醒成功',
    })
  },


  onReceiveAction: function (e) {
    var item = e.currentTarget.dataset.item;
    var url = config.finishOrderUrl;
    var params = {
      orderId: item
    };
    request.requestPost(url, params)
      .then(data => {
        wx.showToast({
          title: '已确认收货',
        })
      }).catch(msg => {
        wx.showToast({
          title: msg,
          icon: 'none'
        })
      });
  },

  /**
   * 点击
   */
  onItemClick :function(event){
    // var item = event.currentTarget.dataset.item;
    var orderid = event.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../orderdetail/orderdetail?orderid='+orderid,
    });
  }

})